package newui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.TrainActionHelp;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.MonthAttendsBean;
import com.ciko.code.bean.PerformanceAddress;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AttendBackReq;
import com.ciko.code.http.Req.AttendReachReq;
import com.ciko.code.http.Req.GetAttendByUserEty;
import com.ciko.code.http.Req.GetMonthAttendsByUserEty;
import com.ciko.code.http.Req.QryPerformanceAddressListEty;
import com.ciko.code.http.Req.QryPerformanceAddressListReq;
import com.ciko.code.utils.DeviceUtil;
import com.ciko.code.views.calendar.CalendarAdapter;
import com.ciko.code.views.calendar.SpecialCalendar;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.humanres.activity.ResMoreActivity;
import com.mywork.WorkUIKit;
import com.mywork.activity.ResRemarkActivity;
import com.shwread.android.qysw.activity.BaseBusinessActivity;
import com.shwread.android.qysw.activity.ReLocationActivity;
import com.shwread.android.qysw10000000.R;
import com.shwread.android.ui.dialog.CommonDialog;
import com.shwread.android.ui.dialog.NoNetDialog;
import com.shwread.http.base.ObjectHttpResponseHandler;
import com.shwread.httpsdk.util.NetUtil;

import org.apache.http.Header;
import org.geometerplus.android.util.QyreaderPerferencesUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.util.DateUtil;
import logic.util.Util;
import logic.util.logutil.LogUtil;

/**
 * Created by ciko on 2015/12/12.
 */
public class NewResCheckActivity extends BaseActicity implements View.OnClickListener,
        AMapLocationListener {

    //    private GestureDetector gestureDetector = null;
    private CalendarAdapter calendarAdapter = null;
    private GridView mGridView = null;
    private TextView topText = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    private boolean isLeapyear = false;  //是否为闰年
    private int daysOfMonth = 0;      //某月的天数
    private int dayOfWeek = 0;        //具体某一天是星期几
    private int lastDaysOfMonth = 0;  //上一个月的总天数
    private int jumpMonth = 0; // 每次滑动，增加或减去一个月,默认为0（即显示当前月）
    private int jumpYear = 0; // 滑动跨越一年，则增加或者减去一年,默认为0(即当前年)
    private int mYear = 0;
    private int mMonth = 0;
    private int mDay = 0;
    private String currentDate = "";
    private String currentYear = "";
    private String currentMonth = "";
    private SpecialCalendar sc = null;
    private RelativeLayout nextMonth; // 下一月文本框
    private RelativeLayout preMonth; // 上一月文本框
    private RelativeLayout rl_rightview;
    private RelativeLayout rl_leftview;
    private String title = "";
    List<MonthAttendsBean> MonthAttendsBeanList = new ArrayList<>();
    List<GetMonthAttendsByUserEty> MonthAttendsByUserList = new ArrayList<>();

    private TextView tvAttendDay,tvAttendStatus,tvQiantuiStateCheck,tvQiantuiTimeCheck,tvQiandaoStateCheck,
            tvQiandaoTimeCheck;
    private ImageView remarksIcon1,remarksIcon2;
    private View rect_QiandaoCheck,rect_QiantuiCheck;


    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;
    private AMapLocation location = null;
    private boolean needManulLocation = false;
    private int CLICKTYPE = 0;     //1点击的是签退，2是签到
    QryPerformanceAddressListEty addressListEty;
    private GetAttendByUserEty getAttendByUser;

    private String reason = "";
    private CommonDialog gpsDialog;
    private ProgressDialog progressDialog;
    private boolean isLocated = false;//判断已定位
    private boolean isGetaddressListEty = false;//判断已获取后台返回值addressListEty
    private CommonDialog dialog;
    private Thread thread;
    private AMapLocation aMapLocation;
    private int attendType = 0;
    Calendar cale = Calendar.getInstance();


    private LinearLayout llQingJia;

    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            if (msg.what == DefaultConsts.MSG_LOCATION_FINISH){
                stopLocation();
            }
        }
    };

    @Override
    protected void initTopBar() {
        if (getIntent().getExtras().containsKey("title")) {
            title = getIntent().getStringExtra("title");
        }
        setTitle(title);
        enableNavLeftImg();

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d", Locale.CHINA);
        currentDate = sdf.format(date); // 当期日期
        mYear = Integer.parseInt(currentDate.split("-")[0]);
        mMonth = Integer.parseInt(currentDate.split("-")[1]);
        mDay = Integer.parseInt(currentDate.split("-")[2]);
        currentYear = mYear + "";
        //得到当前的年份
        currentMonth = mMonth + "";
    }

    @Override
    protected void initLayout() {

        setContentView(R.layout.new_activity_res_check);
        initGpsDialog();
        QyreaderPerferencesUtil util = QyreaderPerferencesUtil.getInstance(this, DefaultConsts.FIRST_TAG);
        if (!Util.gpsIsOpen(this) && util.getShowGpsOpen()) {
            //gpsDialog.show();
            util.setShowGpsOpen(false);
        }

        rl_leftview = (RelativeLayout) findViewById(R.id.left_img);
        rl_rightview = (RelativeLayout) findViewById(R.id.right_img);
        rl_leftview.setOnClickListener(this);
        rl_rightview.setOnClickListener(this);
    }

    private void initGpsDialog() {
        gpsDialog = new CommonDialog(NewResCheckActivity.this) {
            @Override
            protected void clickOk(View v) {
                super.clickOk(v);
                gpsDialog.dismiss();
            }

            @Override
            protected void clickCancel(View v) {
                super.clickCancel(v);
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                NewResCheckActivity.this.startActivityForResult(intent, 0);
            }
        };
        gpsDialog.setTvText("打开“GPS”以便准确获取当前位置");
        gpsDialog.setButtonText("取消");
        gpsDialog.setButtonCancelText("设置");
    }

    @Override
    protected void initView() {
        Activity activity = NewResCheckActivity.this;
        while (activity.getParent() != null) {
            activity = activity.getParent();
        }

        progressDialog = new ProgressDialog(activity);
        progressDialog.setCancelable(true);
        progressDialog.setMessage("正在定位");

        dialog = new CommonDialog(NewResCheckActivity.this) {
            @Override
            protected void clickOk(View v) {
                super.clickOk(v);
            }
        };
        dialog.setTvText("获取当前位置失败，请稍后重试");
        dialog.setButtonText("确定");
        dialog.setBtnCancelGone();
        //加载超时判断
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                BaseBusinessActivity.uiHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (aMapLocation != null && addressListEty == null) {
                            dialog.setTvText("获取考勤范围失败，请稍后重试");
                            progressDialog.dismiss();
                            dialog.show();
                            stopLocation();
                            dialog.setTvText("获取当前位置失败，请稍后重试");
                        }
                    }
                }, 10000);
            }
        });


        sc = new SpecialCalendar();

        mGridView = (GridView) findViewById(R.id.gridview);
//        gestureDetector = new GestureDetector(this, new MyGestureListener());
        calendarAdapter = new CalendarAdapter(this, MonthAttendsBeanList);
//        addGridView();
        mGridView.setAdapter(calendarAdapter);
        topText = (TextView) findViewById(R.id.tv_month);
        addTextToTopTextView(topText);
        initCalender();

        tvAttendDay = (TextView)findViewById(R.id.tvKaoqinDialy);
        tvAttendStatus = (TextView) findViewById(R.id.tvKaoqinStatus);
        tvQiantuiStateCheck = (TextView) findViewById(R.id.tvQiantuiStateCheck);
        tvQiantuiTimeCheck = (TextView) findViewById(R.id.tvQiantuiTimeCheck);
        tvQiandaoStateCheck = (TextView) findViewById(R.id.tvQiandaoStateCheck);
        tvQiandaoTimeCheck = (TextView) findViewById(R.id.tvQiandaoTimeCheck);

        rect_QiandaoCheck = findViewById(R.id.llQiandaoCheck);
        rect_QiantuiCheck = findViewById(R.id.llQiantuiCheck);
        remarksIcon1 = (ImageView) findViewById(R.id.remarks_icon1);
        remarksIcon2 = (ImageView) findViewById(R.id.remarks_icon2);
        llQingJia = (LinearLayout)findViewById(R.id.llQingJia);
        GetAttendByUser();


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        tvAttendDay.setText(getString(R.string.attend_daily, sdf.format(new Date())));
    }

    void GetAttendByUser() {
//        doRequestHttp(new GetAttendByUserReq());
        TrainActionHelp.getAttendByUser(this, new ObjectHttpResponseHandler<GetAttendByUserEty>() {
            @Override
            public void onFailure(int resultCode, Header[] headers, String s, Throwable throwable) {
                if (resultCode == 4001) {

                    rect_QiantuiCheck.setVisibility(View.VISIBLE);
                    rect_QiandaoCheck.setVisibility(View.GONE);
                } else if (resultCode == 8000) {
//            Calendar calendar = Calendar.getInstance(Locale.CANADA);
//            SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
//            tvAttendDay.setText(bActivity.getString(R.string.attend_daily, sdf.format(new Date())));
                    tvAttendStatus.setText(NewResCheckActivity.this.getString(R.string.empty));
                    tvQiandaoTimeCheck.setText("未签到");
                    tvQiantuiTimeCheck.setText("未签退");
                    tvQiandaoStateCheck.setText("");
                    tvQiandaoStateCheck.setText("");

                    rect_QiantuiCheck.setVisibility(View.GONE);
                    rect_QiandaoCheck.setVisibility(View.VISIBLE);
                }
                else if (resultCode == 4003) {
//                    Log.i("TAG", tag);
//                    String str = tag.equals("AttendReachReq") ? "签到" : "签退";
//                    AlertDialog.Builder builder = new AlertDialog.Builder(NewResCheckActivity.this);
//                    builder.setMessage("您更换了手机，请联系管理员后再" + str);
//                    builder.setPositiveButton("确定", null);
//                    builder.show();
                }
                else {
//            loadingView.setVisibility(View.GONE);
//			loadFail.setVisibility(View.VISIBLE);
//			workSv.setVisibility(View.GONE);
                }

//                if (tag.equals("QryPerformanceAddressListReq")) {
//                    progressDialog.dismiss();
//                    CLICKTYPE = 0;
//                }
            }

            @Override
            public void onSuccess(int i, Header[] headers, GetAttendByUserEty obj, String s) {
                initCalender();
                final GetAttendByUserEty getAttendByUserEty = obj;
                getAttendByUser = getAttendByUserEty;
                qianDaoQianTuiBut(getAttendByUserEty);
                String attendType = getAttendByUser.getAttendType();
                String attendTypeName = "";
                switch (attendType) {
                    case "1":
                        attendTypeName = "出差";
                        break;
                    case "2":
                        attendTypeName = "请假";
                        break;
                    case "3":
                        attendTypeName = "其他";
                        break;
                    case "4":
                        attendTypeName = "因公外出";
                        break;
                }
                if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                    tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
                    tvQiandaoStateCheck.setVisibility(View.VISIBLE);
                    tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
                    tvQiantuiStateCheck.setVisibility(View.VISIBLE);
                    switch (getAttendByUserEty.getAttendType()) {
                        case "0":
                            //正常的签到签退逻辑
                            normalAttend(getAttendByUserEty);
                            normalQuit(getAttendByUserEty);
                            break;
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                            if (getAttendByUserEty.getTripLeaveType() == 0) {
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                            tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
                                                    () + "/审批中");
                                        } else {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                            tvQiantuiStateCheck.setText("");
                                            if (getAttendByUserEty.getAttendType().equals("4")){
                                                rect_QiandaoCheck.setVisibility(View.GONE);
                                                rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                                rect_QiantuiCheck.setEnabled(true);
                                                tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                                rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
                                            }
                                        }
                                        break;
                                    case 2:
                                        tvQiandaoTimeCheck.setText("已签到 ");
                                        tvQiandaoStateCheck.setText(attendTypeName);
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 ");
                                            tvQiantuiStateCheck.setText(attendTypeName);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiantuiStateCheck.setText("");
                                        }
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                            tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
                                                    () + "/待签到");
                                        } else {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                        }

                                        break;
                                }
                            } else if (getAttendByUserEty.getTripLeaveType() == 1) {
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 2:
                                        tvQiandaoTimeCheck.setText("已签到 ");
                                        tvQiandaoStateCheck.setText(attendTypeName);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                        normalQuit(getAttendByUserEty);
                                        break;
                                }
                            } else {//请下午假
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1://审批中
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                        break;
                                    case 2://审批通过
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 ");
                                        tvQiantuiStateCheck.setText(attendTypeName);
                                        break;
                                    case 3://审批不同意
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4://审批中
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                        break;
                                }
                            }
                            break;
                    }
                } else {//没有签到的时间reachTimeStr=""
                    tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
                    tvQiandaoStateCheck.setVisibility(View.VISIBLE);
                    tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
                    tvQiantuiStateCheck.setVisibility(View.VISIBLE);
                    if (getAttendByUserEty.getQuitTimeStr().length() == 0) {//未签退
                        tvQiantuiTimeCheck.setText("未签退");
                        tvQiandaoTimeCheck.setText("未签到");
                        tvQiandaoStateCheck.setText("");
                        tvQiantuiStateCheck.setText("");
                        if (getAttendByUserEty.getApprovalStatus()==4||getAttendByUserEty.getApprovalStatus()==1){
                            switch (getAttendByUserEty.getTripLeaveType()){
                                case 0:
                                    tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
                                    tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
                                        tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                                case 1:
                                    tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
                                    tvQiantuiStateCheck.setText("");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiantuiStateCheck.setText("");
                                        tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                                case 2:
                                    tvQiandaoStateCheck.setText("");
                                    tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
//                                    tvQiandaoTimeCheck.setText("未签到");
//                                    tvQiantuiTimeCheck.setText("未签退");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiandaoStateCheck.setText("");
                                        tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                            }
                        }
                    } else {//已经签退
                        switch (getAttendByUserEty.getAttendType()) {
                            case "0":
                                normalAttend(getAttendByUserEty);
                                normalQuit(getAttendByUserEty);
                                break;
                            case "1":
                            case "2":
                            case "3":
                            case "4":
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                        break;
                                    case 2:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 ");
                                        tvQiantuiStateCheck.setText(attendTypeName);
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                        break;
                                }
                                break;
                        }
                    }
                }
                /********************************以下逻辑跟因公外出无关****************************************/
                if (getAttendByUserEty.getApprovalStatus() == 1&&!getAttendByUserEty.getAttendType().equals("4")) {
                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty.getReachTimeStr().split(" ")
                                [1]);
                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty.getQuitTimeStr().split("" +
                                    " ")[1]);
                        }
//                        else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
                    }
//                    else{
//                        tvQiandaoTimeCheck.setText("未签到");
//                        if (getAttendByUserEty.getQuitTimeStr().length()>0&&getAttendByUserEty
// .getQuitTimeStr()!=null){
//                            tvQiantuiTimeCheck.setText("已签退");
//                        }else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
//                    }
                    switch (getAttendByUserEty.getAttendType()) {//审批中的各种情况（请假，出差，因公外出）
                        case "0":
                            break;
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                            switch (getAttendByUserEty.getTripLeaveType()) {
                                case 0://全天
                                    tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                    tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);
                                    }
                                    break;
                                case 1://上午
                                    tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);

                                    }
                                    break;
                                case 2://下午
                                    tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 ");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);
                                    }
                                    break;
                            }
                            break;
                    }
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<GetAttendByUserEty>() {
                }.getType();
            }
        });
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
//        获取指定月所有的记录
//        long time = new Date().getTime();
        cale.set(Calendar.DATE, 3);
//        doRequestHttp(new GetMonthAttendsByUserReq(cale.getTimeInMillis()));
        GetMonthAttendsByUserReq(cale.getTimeInMillis());
    }

    public void GetMonthAttendsByUserReq(long time){
        TrainActionHelp.getMonthAttendsByUser(this, time, new ObjectHttpResponseHandler<Object>() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("fanhui---失败",s);
            }

            @Override
            public void onSuccess(int i, Header[] headers, Object response, String s) {
                Log.e("fanhui---成功",s);
                try {
                    Gson gson = new Gson();
                    JSONObject jsonObject = new JSONObject(s);
                    JSONArray jsonArray = jsonObject.getJSONArray("returnObject");
                    List<GetMonthAttendsByUserEty> getMonthAttendsByUserListResult = gson.fromJson(jsonArray.toString(),
                            new TypeToken<List<GetMonthAttendsByUserEty>>() {}.getType());
                    MonthAttendsByUserList.clear();
                    MonthAttendsByUserList.addAll(getMonthAttendsByUserListResult);
                    initCalender();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
//                List<GetMonthAttendsByUserEty> getMonthAttendsByUserListResult = (List<GetMonthAttendsByUserEty>) response.getReturnObject();
//                MonthAttendsByUserList.clear();
//                MonthAttendsByUserList.addAll(getMonthAttendsByUserListResult);
//                initCalender();
            }

            @Override
            public Type getType() {
                return new TypeToken<Object>() {
                }.getType();
            }
        });
    }


    @Override
    protected void initListener() {
        rect_QiantuiCheck.setOnClickListener(this);
        rect_QiandaoCheck.setOnClickListener(this);
        llQingJia.setOnClickListener(this);
    }

    @Override
    protected void initHttpRequest() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);

        if (v == rl_rightview) {
//                addGridView(); // 添加gridView
            jumpMonth++; // 下一个月

            int stepYear = mYear + jumpYear;
            int stepMonth = mMonth + jumpMonth;
            if (stepMonth > 0) {
                //往下一个月滑动
                if (stepMonth % 12 == 0) {
                    stepYear = mYear + stepMonth / 12 - 1;
                    stepMonth = 12;
                } else {
                    stepYear = mYear + stepMonth / 12;
                    stepMonth = stepMonth % 12;
                }
            } else {
                //往上一个月滑动
                stepYear = mYear - 1 + stepMonth / 12;
                stepMonth = stepMonth % 12 + 12;
                if (stepMonth % 12 == 0) {

                }
            }

            currentYear = String.valueOf(stepYear);
            //得到当前的年份
            currentMonth = String.valueOf(stepMonth);  //得到本月 （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
            Calendar cale = Calendar.getInstance();
            cale.set(Calendar.YEAR, stepYear);
            cale.set(Calendar.MONTH, stepMonth - 1);
            cale.set(Calendar.DATE, 3);
            MonthAttendsByUserList.clear();
            initCalender();
//            doRequestHttp(new GetMonthAttendsByUserReq(cale.getTimeInMillis()));
            GetMonthAttendsByUserReq(cale.getTimeInMillis());

            addTextToTopTextView(topText);
        } else if (v == rl_leftview) {
//                addGridView(); // 添加gridView
            jumpMonth--; // 上一个月
            int stepYear2 = mYear + jumpYear;
            int stepMonth2 = mMonth + jumpMonth;
            if (stepMonth2 > 0) {
                //往下一个月滑动
                if (stepMonth2 % 12 == 0) {
                    stepYear2 = mYear + stepMonth2 / 12 - 1;
                    stepMonth2 = 12;
                } else {
                    stepYear2 = mYear + stepMonth2 / 12;
                    stepMonth2 = stepMonth2 % 12;
                }
            } else {
                //往上一个月滑动
                stepYear2 = mYear - 1 + stepMonth2 / 12;
                stepMonth2 = stepMonth2 % 12 + 12;
                if (stepMonth2 % 12 == 0) {

                }
            }

            currentYear = String.valueOf(stepYear2);
            //得到当前的年份
            currentMonth = String.valueOf(stepMonth2);  //得到本月 （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
            Calendar cale1 = Calendar.getInstance();
            cale1.set(Calendar.YEAR, stepYear2);
            cale1.set(Calendar.MONTH, stepMonth2 - 1);
            cale1.set(Calendar.DATE, 3);
            MonthAttendsByUserList.clear();
            initCalender();
            GetMonthAttendsByUserReq(cale1.getTimeInMillis());
            addTextToTopTextView(topText);
        }else if(v==rect_QiandaoCheck){
            signManipulate(2);
        }else if(v==rect_QiantuiCheck){
            signManipulate(1);
        }else if(v==llQingJia){
            Intent intent = new Intent(this, ResMoreActivity.class);
            intent.putExtra("title", "请假管理");
            startActivity(intent);
        }
    }

    /**
     * 进行签到签退执行的操作
     */
    private void signManipulate(int i) {
        addressListEty = null;
        isGetaddressListEty = false;
        if (!NetUtil.isNetworkConnected(NewResCheckActivity.this)) {
            new NoNetDialog(NewResCheckActivity.this).show();
            return;
        }
        if (!Util.gpsIsOpen(NewResCheckActivity.this)) {
            gpsDialog.show();
            return;
        }
        if (WorkUIKit.getUser().getBranchNo().equals("")) {
            Util.showToast(NewResCheckActivity.this, R.string.params_less_tip);
            return;
        }
        CLICKTYPE = i;
        progressDialog.show();
        doRequestHttp(new QryPerformanceAddressListReq(WorkUIKit.getUser().getBranchNo()));
        thread.run();
        isLocated = false;
        startLocation();
    }


    void startLocation() {
        aMapLocation = null;
        locationClient = new AMapLocationClient(NewResCheckActivity.this);
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置定位监听
        locationClient.setLocationListener(this);
        // 设置定位参数
        locationOption.setNeedAddress(true);
        locationOption.setInterval(1000 * 60);

        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
    }

    private void clickQiantui() {
        if (!checkLocationValid()) {

            dialog = new CommonDialog(NewResCheckActivity.this) {
                @Override
                protected void clickOk(View v) {
                    Intent intent = new Intent(NewResCheckActivity.this, ResRemarkActivity.class);
                    intent.putExtra("title", "备注原因");
                    NewResCheckActivity.this.startActivityForResult(intent, Const.ATTEND_QUIT);
                    dialog.dismiss();
                   /* new AttendOutDialog(bActivity, new AttendOutDialog.SimpleOutListener() {
                        @Override
                        public void out() {
                            toResRemarkForResult("出差", Const.ATTEND_REACH);
                            //出差签到
                        }

                        @Override
                        public void leave() {
                            toResRemarkForResult("请假", Const.ATTEND_REACH);
                        }

                        @Override
                        public void other() {
                            toResRemarkForResult("其他", Const.ATTEND_REACH);
                        }
                    }).show();
                    dialog.dismiss();*/
                }

                @Override
                protected void clickCancel(View v) {
                    dialog.dismiss();
                    Intent intent = new Intent(NewResCheckActivity.this, ReLocationActivity.class);
                    NewResCheckActivity.this.startActivity(intent);
                }
            };
            dialog.setButtonText("备注原因");//----ok
            dialog.setCloseBtnVisiable();
            dialog.setButtonCancelText("重新定位");//--------cancel
            dialog.setTvText("当前定位位置不在考勤范围内，请重新定位或选择原因");
            dialog.show();

//            new AttendOutDialog(bActivity, new AttendOutDialog.SimpleOutListener() {
//                @Override
//                public void out() {
//                    toResRemarkForResult("出差", Const.ATTEND_REACH);
//                }
//
//                @Override
//                public void leave() {
//                    toResRemarkForResult("请假", Const.ATTEND_REACH);
//                }
//
//                @Override
//                public void other() {
//                    toResRemarkForResult("其他", Const.ATTEND_REACH);
//                }
//            }).show();
//            Util.showToast(bActivity, "签退");
//            doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(), location
// .getLatitude(), ""));
        } else {
            String time1 = addressListEty.getEndTime();
            String time2 = DateUtil.getFormatedDateTime(System.currentTimeMillis());
            int min1 = Integer.valueOf(time1.substring(0, 2)) * 60 + Integer.valueOf(time1.substring(3, 5));
            int min2 = Integer.valueOf(time2.substring(0, 2)) * 60 + Integer.valueOf(time2.substring(3, 5));

            if (min1 - 10 > min2 && checkLocationValid()) {
                CommonDialog commonDialog = new CommonDialog(NewResCheckActivity.this) {
                    @Override
                    protected void clickOk(View v) {
                        super.clickOk(v);
                        doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(),
                                location.getLatitude(), "", getPhoneId()));
                    }
                };
                commonDialog.setTvText("现在是早退时间，是否确定签退？");
                commonDialog.show();
            } else {
                doRequestHttp(new AttendBackReq(4, location.getAddress(), location.getLongitude(), location
                        .getLatitude(), "", getPhoneId()));
            }
        }
    }

    //31.1506170000,116.9610680000
    private void clickQiandao() {

        if (checkLocationValid()) {
            //Util.showToast(bActivity, "签到");
            attendType = 0;
            reason = "";
//            doRequestHttp(new AttendReachReq(AttendType.normal.getValue(), location.getAddress(), location
//                    .getLongitude(), location.getLatitude(), "", getPhoneId()));
            doRequestHttp(new AttendReachReq(attendType, location.getAddress(), location
                    .getLongitude(), location.getLatitude(), "", getPhoneId()));
        } else {
            dialog = new CommonDialog(this) {
                @Override
                protected void clickOk(View v) {
                    Intent intent = new Intent(NewResCheckActivity.this, ResRemarkActivity.class);
                    intent.putExtra("title", "备注原因");
                    startActivityForResult(intent, Const.ATTEND_REACH);
                    dialog.dismiss();
                   /* new AttendOutDialog(bActivity, new SimpleOutListener() {
                        @Override
                        public void out() {
                            toResRemarkForResult("出差", Const.ATTEND_REACH);
                            //出差签到
                        }

                        @Override
                        public void leave() {
                            toResRemarkForResult("请假", Const.ATTEND_REACH);
                        }

                        @Override
                        public void other() {
                            toResRemarkForResult("其他", Const.ATTEND_REACH);
                        }
                    }).show();
                    dialog.dismiss();*/
                }

                @Override
                protected void clickCancel(View v) {
                    dialog.dismiss();
                    Intent intent = new Intent(NewResCheckActivity.this, ReLocationActivity.class);
                    startActivity(intent);
                }
            };
            dialog.setButtonText("备注原因");//----ok
            dialog.setCloseBtnVisiable();
            dialog.setButtonCancelText("重新定位");//--------cancel
            dialog.setTvText("当前定位位置不在考勤范围内，请重新定位或选择原因");
            dialog.show();
        }
    }

    private void toResRemarkForResult(String title, int ATTEND) {
        Intent intent = new Intent(this, ResRemarkActivity.class);
        intent.putExtra("title", title);
        startActivityForResult(intent, ATTEND);
    }

    boolean checkLocationValid() {
        boolean result = false;
        if (null == addressListEty) {
            doRequestHttp(new QryPerformanceAddressListReq(WorkUIKit.getUser().getBranchNo()));
        } else {
            if (!addressListEty.getPerformaceList().isEmpty()) {
                for (PerformanceAddress address : addressListEty.getPerformaceList()) {
//                    String dis = getDistance(String.valueOf(31.1506170000), String.valueOf(116
// .9610680000), address.getLat(), address.getLon());
                    String dis = getDistance(String.valueOf(location.getLatitude()), String.valueOf
                            (location.getLongitude()), address.getLat(), address.getLon());
                    LogUtil.d("location", dis);
//					if ((Long.parseLong(dis) * 1000 - Long.parseLong(address.getDistance())) < 0)
                    if ((Double.valueOf(dis) * 1000 - Double.valueOf(address.getDistance())) < 0) {
                        result = true;
                        break;
                    }
                }
            }
        }
        LogUtil.d("location", "" + result);
        return result;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    private static double EARTH_RADIUS = 6378.137;

    /**
     * 根据两个位置的经纬度，来计算两地的距离（单位为KM）
     * 参数为String类型
     *
     * @param lat1Str 用户经度
     * @param lng1Str 用户纬度
     * @param lat2Str 商家经度
     * @param lng2Str 商家纬度
     * @return
     */
    public String getDistance(String lat1Str, String lng1Str, String lat2Str, String lng2Str) {
        Double lat1 = Double.parseDouble(lat1Str);
        Double lng1 = Double.parseDouble(lng1Str);
        Double lat2 = Double.parseDouble(lat2Str);
        Double lng2 = Double.parseDouble(lng2Str);

        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double difference = radLat1 - radLat2;
        double mdifference = rad(lng1) - rad(lng2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2) + Math.cos(radLat1)
                * Math.cos(radLat2) * Math.pow(Math.sin(mdifference / 2), 2)));
        distance = distance * EARTH_RADIUS;
//		distance = Math.round(distance * 10000) / 10000;
        String distanceStr = distance + "";

//		distanceStr = distanceStr.substring(0, distanceStr.indexOf("."));

        return distanceStr;
    }

    private void gotoMoreActivity(String title) {
        Intent intent = new Intent(this, ResMoreActivity.class);
        intent.putExtra("title", title);
        startActivity(intent);
    }


    void initCalender() {
        int stepYear = mYear + jumpYear;
        int stepMonth = mMonth + jumpMonth;
        if (stepMonth > 0) {
            //往下一个月滑动
            if (stepMonth % 12 == 0) {
                stepYear = mYear + stepMonth / 12 - 1;
                stepMonth = 12;
            } else {
                stepYear = mYear + stepMonth / 12;
                stepMonth = stepMonth % 12;
            }
        } else {
            //往上一个月滑动
            stepYear = mYear - 1 + stepMonth / 12;
            stepMonth = stepMonth % 12 + 12;
            if (stepMonth % 12 == 0) {

            }
        }

        currentYear = String.valueOf(stepYear);
        //得到当前的年份
        currentMonth = String.valueOf(stepMonth);  //得到本月 （jumpMonth为滑动的次数，每滑动一次就增加一月或减一月）
//		currentDay = String.valueOf(day_c);  //得到当前日期是哪天

        getCalendar(Integer.parseInt(currentYear), Integer.parseInt(currentMonth));
    }


    //得到某年的某月的天数且这月的第一天是星期几
    public void getCalendar(int year, int month) {
        isLeapyear = sc.isLeapYear(year);              //是否为闰年
        daysOfMonth = sc.getDaysOfMonth(isLeapyear, month);  //某月的总天数
        dayOfWeek = sc.getWeekdayOfMonth(year, month);      //某月第一天为星期几
        lastDaysOfMonth = sc.getDaysOfMonth(isLeapyear, month - 1);  //上一个月的总天数
        Log.e("DAY", isLeapyear + " ======  " + daysOfMonth + "  ============  " + dayOfWeek + "  =========   " + lastDaysOfMonth);
        MonthAttendsBeanList.clear();
        for (int i = 0; i < dayOfWeek; i++) {
            MonthAttendsBean bean = new MonthAttendsBean();
            bean.setDayType(MonthAttendsBean.DayType.LastMonth);
            int temp = lastDaysOfMonth - (dayOfWeek - i) + 1;
            bean.setDay(temp + "");
            MonthAttendsBeanList.add(bean);
        }

        GetMonthAttendsByUserEty[] array = new GetMonthAttendsByUserEty[daysOfMonth];

        for (int i = 0; i < array.length; i++) {
            MonthAttendsBean bean = new MonthAttendsBean();
            bean.setDay(i + 1 + "");
            if ((mDay == i + 1) && (mMonth == Integer.parseInt(currentMonth)))
                bean.setDayType(MonthAttendsBean.DayType.toDay);
            else
                bean.setDayType(MonthAttendsBean.DayType.currentMonth);

            for (int j = 0; j < MonthAttendsByUserList.size(); j++) {
                if (MonthAttendsByUserList.get(j).getAttendDay() != null) {
                    try {
                        Date date = sdf.parse(MonthAttendsByUserList.get(j).getAttendDay());
                        int day = date.getDate();
                        if (day == i + 1) {
                            bean.setUserEty(MonthAttendsByUserList.get(j));
                            break;
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
            }
            MonthAttendsBeanList.add(bean);
        }

        int nextDay = 7 - MonthAttendsBeanList.size() % 7;
        for (int i = 0; i < nextDay; i++) {
            MonthAttendsBean bean = new MonthAttendsBean();
            bean.setDayType(MonthAttendsBean.DayType.LastMonth);
            bean.setDay(i + 1 + "");
            MonthAttendsBeanList.add(bean);
        }

        calendarAdapter.notifyDataSetChanged();

    }

    // 添加头部的年份 闰哪月等信息
    public void addTextToTopTextView(TextView view) {
        StringBuffer textDate = new StringBuffer();
        textDate.append(currentYear).append("年")
                .append(currentMonth).append("月").append("\t");
        view.setText(textDate);
        view.setTextColor(Color.WHITE);
        view.setTypeface(Typeface.DEFAULT_BOLD);
    }



    @Override
    public void onResponseFail(String tag, int resultCode) {
        super.onResponseFail(tag, resultCode);
        if (resultCode == 4001) {

            rect_QiantuiCheck.setVisibility(View.VISIBLE);
            rect_QiandaoCheck.setVisibility(View.GONE);
        } else if (resultCode == 8000) {
//            Calendar calendar = Calendar.getInstance(Locale.CANADA);
//            SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");
//            tvAttendDay.setText(bActivity.getString(R.string.attend_daily, sdf.format(new Date())));
            tvAttendStatus.setText(NewResCheckActivity.this.getString(R.string.empty));
            tvQiandaoTimeCheck.setText("未签到");
            tvQiantuiTimeCheck.setText("未签退");
            tvQiandaoStateCheck.setText("");
            tvQiandaoStateCheck.setText("");

            rect_QiantuiCheck.setVisibility(View.GONE);
            rect_QiandaoCheck.setVisibility(View.VISIBLE);
        } else if (resultCode == 4003) {
            Log.i("TAG", tag);
            String str = tag.equals("AttendReachReq") ? "签到" : "签退";
            AlertDialog.Builder builder = new AlertDialog.Builder(NewResCheckActivity.this);
            builder.setMessage("您更换了手机，请联系管理员后再" + str);
            builder.setPositiveButton("确定", null);
            builder.show();
        } else {
//            loadingView.setVisibility(View.GONE);
//			loadFail.setVisibility(View.VISIBLE);
//			workSv.setVisibility(View.GONE);
        }

        if (tag.equals("QryPerformanceAddressListReq")) {
            progressDialog.dismiss();
            CLICKTYPE = 0;
        }
    }

    @Override
    public void onResponseSuccess(final BaseResponse<?> response, final String tag) {
        super.onResponseSuccess(response, tag);
//        Log.e("tag", tag);
        switch (tag) {
            case "GetMonthAttendsByUserReq":
                List<GetMonthAttendsByUserEty> getMonthAttendsByUserListResult = (List<GetMonthAttendsByUserEty>) response.getReturnObject();
                MonthAttendsByUserList.clear();
                MonthAttendsByUserList.addAll(getMonthAttendsByUserListResult);
                initCalender();
//                resListOverWorkAdapter.notifyDataSetChanged();
                break;
            case "GetAttendByUserReq":
//                loadingView.setVisibility(View.GONE);
//                loadFail.setVisibility(View.GONE);
//                workSv.setVisibility(View.VISIBLE);
                initCalender();
                final GetAttendByUserEty getAttendByUserEty = (GetAttendByUserEty) response
                        .getReturnObject();
                getAttendByUser = getAttendByUserEty;
                qianDaoQianTuiBut(getAttendByUserEty);
                String attendType = getAttendByUser.getAttendType();
                String attendTypeName = "";
                switch (attendType) {
                    case "1":
                        attendTypeName = "出差";
                        break;
                    case "2":
                        attendTypeName = "请假";
                        break;
                    case "3":
                        attendTypeName = "其他";
                        break;
                    case "4":
                        attendTypeName = "因公外出";
                        break;
                }
                if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                    tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
                    tvQiandaoStateCheck.setVisibility(View.VISIBLE);
                    tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
                    tvQiantuiStateCheck.setVisibility(View.VISIBLE);
                    switch (getAttendByUserEty.getAttendType()) {
                        case "0":
                            //正常的签到签退逻辑
                            normalAttend(getAttendByUserEty);
                            normalQuit(getAttendByUserEty);
                            break;
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                            if (getAttendByUserEty.getTripLeaveType() == 0) {
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                            tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
                                                    () + "/审批中");
                                        } else {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                            tvQiantuiStateCheck.setText("");
                                            if (getAttendByUserEty.getAttendType().equals("4")){
                                                rect_QiandaoCheck.setVisibility(View.GONE);
                                                rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                                rect_QiantuiCheck.setEnabled(true);
                                                tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                                rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
                                            }
                                        }
                                        break;
                                    case 2:
                                        tvQiandaoTimeCheck.setText("已签到 ");
                                        tvQiandaoStateCheck.setText(attendTypeName);
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 ");
                                            tvQiantuiStateCheck.setText(attendTypeName);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiantuiStateCheck.setText("");
                                        }
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        if (getAttendByUserEty.getQuitTimeStr()
                                                .length() > 0) {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                            tvQiantuiStateCheck.setText(getAttendByUserEty.getQuitTypeName
                                                    () + "/待签到");
                                        } else {
                                            tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                    .getReachTimeStr().split(" ")[1]);
                                            tvQiantuiTimeCheck.setText("未签退");
                                            tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                        }

                                        break;
                                }
                            } else if (getAttendByUserEty.getTripLeaveType() == 1) {
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 2:
                                        tvQiandaoTimeCheck.setText("已签到 ");
                                        tvQiandaoStateCheck.setText(attendTypeName);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        tvQiandaoStateCheck.setText(attendTypeName + "/待签到");
                                        normalQuit(getAttendByUserEty);
                                        break;
                                }
                            } else {//请下午假
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1://审批中
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                        break;
                                    case 2://审批通过
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 ");
                                        tvQiantuiStateCheck.setText(attendTypeName);
                                        break;
                                    case 3://审批不同意
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4://审批中
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                        break;
                                }
                            }
                            break;
                    }
                } else {//没有签到的时间reachTimeStr=""
                    tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
                    tvQiandaoStateCheck.setVisibility(View.VISIBLE);
                    tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
                    tvQiantuiStateCheck.setVisibility(View.VISIBLE);
                    if (getAttendByUserEty.getQuitTimeStr().length() == 0) {//未签退
                        tvQiantuiTimeCheck.setText("未签退");
                        tvQiandaoTimeCheck.setText("未签到");
                        tvQiandaoStateCheck.setText("");
                        tvQiantuiStateCheck.setText("");
                        if (getAttendByUserEty.getApprovalStatus()==4||getAttendByUserEty.getApprovalStatus()==1){
                            switch (getAttendByUserEty.getTripLeaveType()){
                                case 0:
                                    tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
                                    tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
                                        tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                                case 1:
                                    tvQiandaoStateCheck.setText(attendTypeName+"/审批中");
                                    tvQiantuiStateCheck.setText("");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiantuiStateCheck.setText("");
                                        tvQiandaoStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                                case 2:
                                    tvQiandaoStateCheck.setText("");
                                    tvQiantuiStateCheck.setText(attendTypeName+"/审批中");
//                                    tvQiandaoTimeCheck.setText("未签到");
//                                    tvQiantuiTimeCheck.setText("未签退");
                                    if (getAttendByUserEty.getAttendType().equals("4")&&getAttendByUserEty.getApprovalStatus()==4){
                                        tvQiandaoStateCheck.setText("");
                                        tvQiantuiStateCheck.setText(attendTypeName+"/待签到");
                                    }
                                    break;
                            }
                        }
                    } else {//已经签退
                        switch (getAttendByUserEty.getAttendType()) {
                            case "0":
                                normalAttend(getAttendByUserEty);
                                normalQuit(getAttendByUserEty);
                                break;
                            case "1":
                            case "2":
                            case "3":
                            case "4":
                                switch (getAttendByUserEty.getApprovalStatus()) {
                                    case 1:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                        break;
                                    case 2:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 ");
                                        tvQiantuiStateCheck.setText(attendTypeName);
                                        break;
                                    case 3:
                                        normalAttend(getAttendByUserEty);
                                        normalQuit(getAttendByUserEty);
                                        break;
                                    case 4:
                                        normalAttend(getAttendByUserEty);
                                        tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                .getQuitTimeStr().split(" ")[1]);
                                        tvQiantuiStateCheck.setText(attendTypeName + "/待签到");
                                        break;
                                }
                                break;
                        }
                    }
                }
                /********************************以下逻辑跟因公外出无关****************************************/
                if (getAttendByUserEty.getApprovalStatus() == 1&&!getAttendByUserEty.getAttendType().equals("4")) {
                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty.getReachTimeStr().split(" ")
                                [1]);
                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty.getQuitTimeStr().split("" +
                                    " ")[1]);
                        }
//                        else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
                    }
//                    else{
//                        tvQiandaoTimeCheck.setText("未签到");
//                        if (getAttendByUserEty.getQuitTimeStr().length()>0&&getAttendByUserEty
// .getQuitTimeStr()!=null){
//                            tvQiantuiTimeCheck.setText("已签退");
//                        }else{
//                            tvQiantuiTimeCheck.setText("未签退");
//                        }
//                    }
                    switch (getAttendByUserEty.getAttendType()) {//审批中的各种情况（请假，出差，因公外出）
                        case "0":
                            break;
                        case "1":
                        case "2":
                        case "3":
                        case "4":
                            switch (getAttendByUserEty.getTripLeaveType()) {
                                case 0://全天
                                    tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                    tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);
                                    }
                                    break;
                                case 1://上午
                                    tvQiandaoStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 " + getAttendByUserEty
                                                    .getQuitTimeStr().split(" ")[1]);
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.icon_qiantui);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);

                                    }
                                    break;
                                case 2://下午
                                    tvQiantuiStateCheck.setText(attendTypeName + "/审批中");
                                    if (getAttendByUserEty.getReachTimeStr().length() > 0) {
                                        tvQiandaoTimeCheck.setText("已签到 " + getAttendByUserEty
                                                .getReachTimeStr().split(" ")[1]);
                                        rect_QiandaoCheck.setVisibility(View.GONE);
                                        if (getAttendByUserEty.getQuitTimeStr().length() > 0) {
                                            tvQiantuiTimeCheck.setText("已签退 ");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(false);
                                            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
                                        } else {
                                            tvQiantuiTimeCheck.setText("未签退");
                                            rect_QiandaoCheck.setVisibility(View.GONE);
                                            rect_QiantuiCheck.setVisibility(View.VISIBLE);
                                            rect_QiantuiCheck.setEnabled(true);
                                        }
                                    } else {
                                        tvQiandaoTimeCheck.setText("未签到");
                                        rect_QiandaoCheck.setVisibility(View.VISIBLE);
                                        rect_QiantuiCheck.setVisibility(View.GONE);
                                    }
                                    break;
                            }
                            break;
                    }
                }
                break;
            case "QryPerformanceAddressListReq":
                addressListEty = (QryPerformanceAddressListEty) response.getReturnObject();
                isGetaddressListEty = true;
                if (isLocated) {
                    progressDialog.dismiss();
                    if (CLICKTYPE == 1) {
                        clickQiantui();
                    } else if (CLICKTYPE == 2) {
                        clickQiandao();
                    }
                    CLICKTYPE = 0;
                }
                GetAttendByUser();
                break;
            case "AttendReachReq":
                String msg1 = (String) response.getReturnObject();
                Util.showToast(NewResCheckActivity.this, msg1);
                //刷新页面
                GetAttendByUser();
                break;
            case "AttendBackReq":
                String msg2 = (String) response.getReturnObject();
                Util.showToast(NewResCheckActivity.this, msg2);
                //刷新页面
                GetAttendByUser();
                break;
        }
    }


    public void qianDaoQianTuiBut(GetAttendByUserEty getAttendByUser) {
        rect_QiandaoCheck.setVisibility(View.VISIBLE);
        rect_QiantuiCheck.setVisibility(View.GONE);
//        String reachTime = Long.toString(getAttendByUser.getReachTime());
//        String quitTime = Long.toString(getAttendByUser.getQuitTime());
//        if (reachTime == null || reachTime == "" || reachTime.equals("null")) {
//            rect_QiandaoCheck.setVisibility(View.VISIBLE);
//            rect_QiantuiCheck.setVisibility(View.GONE);
//        } else {
//            rect_QiandaoCheck.setVisibility(View.GONE);
//            if (quitTime == null || quitTime == "" || quitTime.equals("null")) {
//                rect_QiantuiCheck.setVisibility(View.VISIBLE);
//            } else {
//                rect_QiantuiCheck.setVisibility(View.VISIBLE);
//                rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
//                rect_QiantuiCheck.setEnabled(false);
//            }
//        }


        if (getAttendByUser.getReachTimeStr().length() > 0 && getAttendByUser.getReachTimeStr() != null) {
//            tvQiandaoTimeCheck.setText("已签到");
            rect_QiandaoCheck.setVisibility(View.GONE);
            rect_QiantuiCheck.setVisibility(View.VISIBLE);
            if (getAttendByUser.getQuitTimeStr().length() > 0 && getAttendByUser.getQuitTimeStr() != null) {
//                rect_QiandaoCheck.setVisibility(View.VISIBLE);
//                rect_QiantuiCheck.setVisibility(View.GONE);
                rect_QiantuiCheck.setEnabled(false);
                rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
            }
        }
    }

    //正常的签到----已签到过了
    public void normalAttend(GetAttendByUserEty getAttendByUser) {
        tvQiandaoTimeCheck.setVisibility(View.VISIBLE);
        tvQiandaoStateCheck.setVisibility(View.VISIBLE);
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        if (getAttendByUser.getAttendType().equals("0")) {
            if (getAttendByUser.getReachTimeStr().length() > 0) {
                tvQiandaoTimeCheck.setText("已签到 " + df.format(new Date(
                        getAttendByUser.getReachTime())));
//                tvQiandaoStateCheck.setText();
            } else {
                tvQiandaoTimeCheck.setText("未签到 ");
            }
        } else {
            if (getAttendByUser.getReachTimeStr().length() > 0) {
                tvQiandaoTimeCheck.setText("已签到 " + df.format(new Date(
                        getAttendByUser.getReachTime())));
                rect_QiandaoCheck.setVisibility(View.GONE);
                rect_QiantuiCheck.setVisibility(View.VISIBLE);
                rect_QiantuiCheck.setEnabled(false);
                rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
            } else {
                tvQiandaoTimeCheck.setText("未签到 ");
                tvQiandaoStateCheck.setText("");
                rect_QiandaoCheck.setVisibility(View.VISIBLE);
                rect_QiantuiCheck.setVisibility(View.GONE);
            }
        }
        if (getAttendByUser.getReachType().equals("0")) {
            tvQiandaoStateCheck.setText("签到成功");
        } else if (getAttendByUser.getReachType().equals("1")) {
            tvQiandaoStateCheck.setText("迟到");
        } else if (getAttendByUser.getReachType().equals("3")){
            tvQiandaoStateCheck.setText("其他");
        }
    }

    //正常的签退
    public void normalQuit(GetAttendByUserEty getAttendByUser) {
        tvQiantuiTimeCheck.setVisibility(View.VISIBLE);
        tvQiantuiStateCheck.setVisibility(View.VISIBLE);
        if (getAttendByUser.getQuitTimeStr().length()==0) {
            tvQiantuiTimeCheck.setText("未签退");
            tvQiantuiStateCheck.setText("");
            rect_QiantuiCheck.setEnabled(true);
        } else {
            SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
            tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
                    getAttendByUser.getQuitTime())));
            if (getAttendByUser.getQuitType().equals("0")) {
                tvQiantuiTimeCheck.setText("已签退 "+getAttendByUser.getQuitTimeStr().split(" ")[1]);
                tvQiantuiStateCheck.setText("正常");
            } else if (getAttendByUser.getQuitType().equals("2")) {
                tvQiantuiStateCheck.setText("早退");
                tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
                        getAttendByUser.getQuitTime())));
            } else {
                tvQiantuiStateCheck.setText("签退异常");
                tvQiantuiTimeCheck.setText("已签退 " + df.format(new Date(
                        getAttendByUser.getQuitTime())));
            }
            rect_QiantuiCheck.setEnabled(false);
            rect_QiantuiCheck.setBackgroundResource(R.drawable.qiantui_bg);
        }
    }

//    Drawable getDrawable(int resId) {
//        return NewResCheckActivity.this.getResources().getDrawable(resId);
//    }

    public static String getChineseWeekday(Calendar paramCalendar) {
        switch (paramCalendar.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                return "星期日";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            case 7:
                return "星期六";
        }
        return "未知";
    }

    public static String getChineseDate(Calendar paramCalendar) {
        int i = paramCalendar.get(Calendar.YEAR);
        int j = paramCalendar.get(Calendar.MONTH);
        int k = paramCalendar.get(Calendar.DAY_OF_MONTH);
        StringBuffer localStringBuffer = new StringBuffer();
        localStringBuffer.append(i).append("/").append(j + 1).append("/").append(k);
        return localStringBuffer.toString();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        this.aMapLocation = aMapLocation;
        if (null != aMapLocation) {
            Message msg = Message.obtain();
            msg.obj = aMapLocation;
            msg.what = DefaultConsts.MSG_LOCATION_FINISH;
            handler.sendMessage(msg);
            //            Logger.i(getLocationStr(aMapLocation));
            this.location = aMapLocation;
            isLocated = true;
            if (isGetaddressListEty) {
                progressDialog.dismiss();
                if (CLICKTYPE == 1) {
                    clickQiantui();
                } else if (CLICKTYPE == 2) {
                    clickQiandao();
                }
                CLICKTYPE = 0;
            }
        } else {
            progressDialog.dismiss();
            CLICKTYPE = 0;
            dialog.show();
        }
    }

    void stopLocation() {
        if (null != locationClient) {
            locationClient.stopLocation();
        }
    }

    /**
     * 获取手机识别码
     */
    private String getPhoneId() {
        DeviceUtil deviceUtil = new DeviceUtil(NewResCheckActivity.this);
        if (!Util.isEmpty(deviceUtil.getImei())) {
            return deviceUtil.getImei();
        }
        return deviceUtil.getAndroidId();
    }
}
